/**
 * @file entry
 * @author ashen9(ashen9@qq.com)
 */

import Vue from 'vue';
import Meta from 'vue-meta';
import {isAndroid, isiOS} from '@/common/device';

import {createRouter} from '@/.lavas/router';
import {createStore} from '@/.lavas/store';
import AppComponent from './App.vue';
import {storePermission} from '@/common/permission';
import nativeAction from '@/common/native';

import 'mint-ui/lib/style.css';
import '@/assets/css/common.scss';
import MintUI from 'mint-ui';
import VeeValidate from '@/common/validate.js';
Vue.filter('permissions', storePermission);
Vue.use(VeeValidate, {
    locale: 'zh_CN'
});
Vue.use(MintUI);
Vue.use(Meta);

Vue.config.productionTip = false;

export function createApp() {
    let router = createRouter();
    let store = createStore();
    router.beforeEach((to, from, next) => {
        if ((isAndroid || isiOS)) {
            if (from.path === '/') {
                if (to.path.includes('home') || to.path.includes('risk')) { //安卓默认跳至home页或者点击推送的系统容消息打开应用，已登录状态从native获取登录信息
                    nativeAction.getLogin(to, from);
                }
            }
        }
        if (to.matched.some(record => record.meta.requiresAuth) && !store.state.login.userToken) {
            next({
                path: '/login',
                query: {redirect: to.fullPath}// 将跳转的路由path作为参数
            });
        } else {
            next();
        }
    });
    let App = Vue.extend({
        router,
        store,
        ...AppComponent
    });
    return {App, router, store};
}

if (module.hot) {
    module.hot.accept();
}
